ExtGames.FlinchGame = function(config) {
	
	this.wildToPlay = null;
	
	ExtGames.FlinchGame.superclass.constructor.call(this, config);

};

Ext.extend(ExtGames.FlinchGame, ExtGames.Game, {
	
	newGame: function() {
	    
		  console.log('Starting New Game');
		  	  
		  var num = util.getTurnIndex();
		  
		  for (var i = 0; i < 10; i++) {
			  for (var j = 0; j < this.playdecks.length; j++) {
				  			  			  
				  var card = this.pack.getTopCard();
			      card.move(this.playdecks[num]);
			      		      
			      num++;
			      if (num >= this.playdecks.length) {
			    	  num = 0;
			      }
			  }
		  }
		  
		  this.populateHand();
		    
		  console.log('Hand Pop');
		  
		    while (c = this.pack.getTopCard()) {
		        c.move(this.decks[0]);
		      }
		    
		    		
		    if (util.isTurn()) {
		    	
		    }
		    else {
		    	this.turn = TURN_END;
		    }
		    	    
		    console.log('New Game Started');
	  },
	
	nextTurn: function() {
		if (util.isTurn()) {
			if (this.hands[0].getNumCards() == 5) {
				var card = this.decks[0].getTopCard();
			    card.move(this.hands[0]);
			    card.reveal();
			}
			else {
				while (this.hands[0].getNumCards() < 5) {
					var card = this.decks[0].getTopCard();
				    card.move(this.hands[0]);
				    card.reveal();
				}
			}
		}
		
		ExtGames.FlinchGame.superclass.nextTurn.call(this);
	},
  	
  	isGameEnd: function() {
  		return true; 
  	},

	getNumberOfCardsToDeal: function() {
		return 5;
	},
	
	getToolbarButtons: function() {
		var buttons = [];
		
		return buttons;
	},
	
	getScores: function() {
		  var scorecard = [];
		  
		  for (var i = 0; i < this.hands.length; i++) {
			  cards = this.hands[i].toObject();
			  		  
			  points = 0;
			  for (var j = 0; j < cards.length; j++) {
				  points += getCardPoints(cards[j]);
			  }
			  scorecard[i] = points;
		  }
		  
		  return scorecard;
	},
	
	dropCard: function(stack, card, e) {	  
	  	  
		  var from = card.location.itemId;
		  var to = stack.itemId;
		  
		  console.log('DROP CARD from '+from+' to '+to);
		  	  
		  if (isMyHand(from) && isMyHand(to)) {
		  	  this.hands[0].rearrange(card, e);
		  	  return;
		  }
		  
		  if (this.turn == TURN_START) {
			  if ((isMyHand(from) || cCmp(from, 'reserve') || cCmp(from, 'playdeck')) && isDiscardPile(to)) {
				  
				  if (!this.isValidPlay(card, stack)) {
					  return;
				  }
				  
				  card.move(stack);
				  
				  var s = {};
				  s.from = from;
				  s.to = to;
				  s.card = card.toObject();
				  util.discard(s);
				  
				  if (card.getNumberIndex() == (card.numbers.length-1)) {
					  stack.destroyCards();
				  }
				  
				  if (this.playdecks[0].getNumCards() == 0) {
					  this.endRound();
				  }
				  else if (this.hands[0].getNumCards() == 0) {
					  for (var i = 0; i < 5; i++) {
						  var card = this.decks[0].getTopCard();
						  card.move(this.hands[0]);
						  card.reveal();
						  
						  var s = {};
						  s.from = 'deck_0';
						  s.card = card.toObject();
						  util.drawCard(s);
					  }
				  }
			  }
			  else if (isMyHand(from) && cCmp(to, 'reserve')) {
				  
				  card.move(stack);
				  
				  var s = {};
				  s.card = card.toObject();		
				  s.to = to;
				  util.discardToReserve(s);
				  
				  util.endTurn();
			  }
				  
		  }

	  },
	  
	  isValidPlay: function(card, discardPile) {
		  var lastCard = discardPile.getLastCard();
		  if (lastCard) {
			  if (card.joker == true) {
				  return true;
			  }
			  else if (lastCard.joker == true) {
				  var i = 1;
				  while (lastCard.joker == true) {
					  lastCard = discardPile.getLastCard(i);
					  i++;
				  }
				  if (card.getNumberIndex() == (lastCard.getNumberIndex()+i)) {
					  return true;
				  }
			  }
			  else if (card.getNumberIndex() == (lastCard.getNumberIndex()+1)) {
				  return true;
			  }
		  }
		  else if (card.getNumberIndex() == 0) {
			  return true;
		  }
		  return false;
	  },
	  
	  discardedCard: function(details) {
		  var turnIndex = util.getTurnIndex();
		  
		  var hand = this.hands[turnIndex];
		  if (cCmp(details.from, 'reserve')) {
			  var idx = eval('('+details.from.substring(8)+')');
			  hand = this.reserves[turnIndex][idx];
		  }
		  else if (cCmp(details.from, 'playdeck')) {
			  hand = this.playdecks[turnIndex];
		  }
		  var card = hand.findCard(details.card);
		  
		  var pile = null;
		  for (var i = 0; i < this.discardpiles.length; i++) {
			  if (this.discardpiles[i].itemId == details.to) {
				  pile = this.discardpiles[i];
			  }
		  }
		  
		  card.move(pile);
		  
		  card.reveal();
		  
		  if (card.getNumberIndex() == (card.numbers.length-1)) {
			  pile.destroyCards();
		  }
	  },
	  
	  discardedToReserve: function(details) {
		  var turnIndex = util.getTurnIndex();
		  
		  var hand = this.hands[turnIndex];
		  var card = hand.findCard(details.card);
		  
		  var idx = eval('('+details.to.substring(9)+')');
		  var pile = this.reserves[turnIndex][idx];
		  
		  card.move(pile);
		  
		  card.reveal();
	  },
	  
	  getNumberOfDiscardPiles: function() {
		  return 4;
	  },
	  
	  getAddHandComp: function() {
		  	var opp = util.getOpponents();
		  
			var comps = [];
			comps[0] = [];
			for (var i = 0; i < opp.length; i++) {
				comps[i+1] = [];
			}
		  
		  	this.playdecks = [];
		  	
		  	this.playdecks.push(new ExtGames.FlinchPlayDeck({
				itemId: 'playdeck'
		    }));
		  	
		  	comps[0].push(this.playdecks[0]);
			
			for (var i = 0; i < opp.length; i++) {
				
				this.playdecks.push(new ExtGames.FlinchPlayDeck({
					itemId: 'playdeck_'+i
			    }));
				comps[i+1].push(this.playdecks[i+1]);
			}
		  
		  
		  
			this.reserves = [];
			this.reserves[0] = [];
				
			var i = 0;
				
			for (i = 0; i < 5; i++) {
				this.reserves[0][i] = new ExtGames.FlinchReservePile({
					itemId: 'reserve_' + i,
					opp: 0,
					listeners: {
			            'carddropped': {
			            	fn: this.dropCard,
			            	scope: this,
			            }
			          }
			        });  
				
				comps[0].push(this.reserves[0][i]);
			}
							
			for (var j = 0; j < opp.length; j++) {
				this.reserves[j+1] = [];
				for (i = 0; i < 5; i++) {
					this.reserves[j+1][i] = new ExtGames.FlinchReservePile({
						itemId: 'reserve_'+j+'_' + i,
						opp: 1,
						listeners: {
				            'carddropped': {
				            	fn: this.dropCard,
				            	scope: this,
				            }
				          }
				        });  
					
					comps[j+1].push(this.reserves[j+1][i]);
				}
			}
			
			console.log('Created Reserve Piles');			
			
			return comps;
		}

});